package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.exception.CustomException;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.SetMeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{

    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;



    @Override
    public void insert(Category category) {
        this.save(category);
    }

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Dish> lqwD = new LambdaQueryWrapper<Dish>();
        lqwD.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(lqwD);
        if(count1>0){
            throw new CustomException("当前分类下关联菜品,不能删除!");
        }
        LambdaQueryWrapper<SetMeal> lqwS = new LambdaQueryWrapper<SetMeal>();
        lqwS.eq(SetMeal::getCategoryId,id);
        int count2 =setMealService.count(lqwS);
        if(count2>0){
            throw new CustomException("当前分类下关联套餐,不能删除!");
        }
        super.removeById(id);
    }



    @Override
    public void update(Category category) {
        this.updateById(category);
    }



    @Override
    public List<Category> list(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getType,type);
        return this.list(lqw);
    }





}
